package com.sch.wanandroid.ui.addshare

import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.coder.zzq.smartshow.toast.SmartToast
import com.sch.wanandroid.R
import com.sch.wanandroid.base.BaseActivity
import com.sch.wanandroid.databinding.ActivityAddArticleBinding
import kotlinx.android.synthetic.main.activity_add_article.*
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_login.indicatorView

/**
 * Created by Sch.
 * Date: 2020/12/23
 * description:
 */
class AddShareActivity : BaseActivity() {
    private lateinit var addShareViewModel: AddShareViewModel
    private lateinit var databinding: ActivityAddArticleBinding

    override fun getLayoutId(): Int {
        databinding = DataBindingUtil.setContentView(this, R.layout.activity_add_article)
        databinding.lifecycleOwner = this
        return 0
    }

    override fun init(savedInstanceState: Bundle?) {
        initTitle("分享")
        addShareViewModel = ViewModelProvider(this).get(AddShareViewModel::class.java)
        databinding.data = addShareViewModel
        databinding.llShare.setOnClickListener {
            if (addShareViewModel.titleString.value.isNullOrEmpty()) {
                SmartToast.fail("标题不能为空")
                return@setOnClickListener
            }
            if (addShareViewModel.linkString.value.isNullOrEmpty()) {
                SmartToast.fail("链接不能为空")
                return@setOnClickListener
            }
            setViewStatus(false)
            addShareViewModel.addShare()
        }
        addShareViewModel.addSuccess.observe(this) {
            if (it) {
                SmartToast.success("分享成功")
                finish()
            }
        }
        addShareViewModel.errorMsg.observe(this) {
            setViewStatus(true)
            SmartToast.fail(it)
        }
    }

    private fun setViewStatus(lockStatus: Boolean) {
        databinding.llShare.isEnabled = lockStatus
        databinding.etTitle.isEnabled = lockStatus
        databinding.etLink.isEnabled = lockStatus
        if (lockStatus) {
            databinding.tvShare.visibility = View.VISIBLE
            databinding.indicatorView.visibility = View.GONE
            databinding.indicatorView.hide()
        } else {
            databinding.tvShare.visibility = View.GONE
            databinding.indicatorView.visibility = View.VISIBLE
            databinding.indicatorView.show()
        }
    }

}